package lc.oa.net.http;

import android.content.Context;
import android.util.Log;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import lc.oa.net.http.request.GsonRequest;

@RunWith(AndroidJUnit4.class)
public class HttpHelperTest {
    /**
     * Test 原生态请求
     */
    @Test
    public void testRequest() {
        Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

        Log.i("-------------testRequest--------------------------","测试");
        //String url = "https://www.baidu.com/";
        String url="https://oa-dev.lecarlink.com/lc.oa.service/test/test";
        Request<Object> reqQueue = new Request<Object>(Request.Method.GET, url, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("------请求日志-------", "错误", error);
            }
        }) {
            @Override
            protected Response<Object> parseNetworkResponse(NetworkResponse response) {
                try {
                    String result = null;
                    try {
                        result = new String(response.data,
                                HttpHeaderParser.parseCharset(response.headers));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    return Response.success(result,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            /**
             * 响应逻辑处理
             * @param response
             */
            @Override
            protected void deliverResponse(Object response) {
                Log.e("---------响应日志-------------", "请求成功!");
                Log.d("---------响应日志内容-------------", response.toString());
            }
        };
        HttpQueueHelper.getInstance(context).addToRequestQueue(reqQueue);

        RequestQueue requestQueue = HttpQueueHelper.getInstance(context).getRequestQueue();

    }

    /**
     * 模拟Json请求
     */
    @Test
    public void testGsonRequest(){
        Context context=InstrumentationRegistry.getInstrumentation().getTargetContext();
        String url="https://oa-dev.lecarlink.com/lc.oa.service/test/test";
        Response.Listener listerSuccess = new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                Log.e("---------响应日志-------------", "请求成功!");
            }
        };
        Response.ErrorListener errLister=new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("---------响应日志-------------", "响应失败!",error);
                error.printStackTrace();
            }
        };

        Map<String, String> header=new HashMap<>();
        header.put("Content-Type","application/json");
        header.put("Content-Type","text/html;charset=UTF-8");

        GsonRequest<String> gsonRequest=new GsonRequest<String>(url,String.class,header,listerSuccess,errLister);
        HttpQueueHelper.getInstance(context).addToRequestQueue(gsonRequest);

    }

}
